package net.topikachu.dynamic.faces;

import java.util.LinkedList;

import javax.faces.component.UIComponent;

import com.sun.facelets.FaceletContext;

public class WidgetPlaceHolderStack {
	private LinkedList<UIComponent> stack = new LinkedList<UIComponent>();
	public static String WIDGET_PLACEHOLDER_STACK = "widget.placeholder.stack";

	public void push(UIComponent c) {
		stack.addLast(c);
	}

	public UIComponent pop() {
		return stack.removeLast();
	}

	public boolean hasPlaceHolder() {
		return stack.size() != 0;
	}

	public static WidgetPlaceHolderStack getStack(FaceletContext ctx) {
		WidgetPlaceHolderStack stack = (WidgetPlaceHolderStack) ctx
				.getFacesContext().getExternalContext().getRequestMap().get(
						WidgetPlaceHolderStack.WIDGET_PLACEHOLDER_STACK);
		return stack;
	}

	public static void removeStack(FaceletContext ctx) {
		ctx.getFacesContext().getExternalContext().getRequestMap().remove(
				WidgetPlaceHolderStack.WIDGET_PLACEHOLDER_STACK);
	}

	public static void createStack(FaceletContext ctx) {
		WidgetPlaceHolderStack stack = new WidgetPlaceHolderStack();
		ctx.getFacesContext().getExternalContext().getRequestMap().put(
				WidgetPlaceHolderStack.WIDGET_PLACEHOLDER_STACK, stack);
	}
}
